home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17424 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  68 lines

  1. Path: news.belwue.de!uzwil!kuehl
  2. From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ Gurus! Is it correct?
  5. Date: 15 Apr 1996 21:55:41 GMT
  6. Organization: FakultΣt fⁿr Mathematik und Informatik
  7. Message-ID: <4kugkt$j3s@news.BelWue.DE>
  8. References: <4eqvtg$cg5@israel-info.datasrv.co.il> <4etnju$6gn@rolaids.frco.com> <4f0n8s$a3b@news2.ios.com> <311403bf.169980736@nntp.ix.netcom.com> <4f2koo$q5a@news2.ios.com>
  9. Reply-To: dietmar.kuehl@uni-konstanz.de
  10. NNTP-Posting-Host: uzwil.informatik.uni-konstanz.de
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Hi,
  14.  
  15. Vlastimil Adamovsky (vlad@gramercy.ios.com) wrote:
  16. : miker3@ix.netcom.com (Mike Rubenstein) wrote:
  17.  
  18. : >vlad@gramercy.ios.com (Vlastimil Adamovsky) wrote:
  19. : >> I don't think the virtual destructor is necessary in this specific
  20. : >> case where you have no added data in subclasses. 
  21.  
  22. : >Why do you think having data has anything to do with it?  From draft
  23. : >5.3.5:
  24.  
  25. : >    In the first alternative (delete object), if the static type
  26. : >    of the operand is different from its dynamic type, the static
  27. : >    type shall be a base  class of the operand's dynamic type
  28. : >    and the static type shall have a virtual destructor or the
  29. : >    behavior is undefined.
  30.  
  31.  
  32. : I prefer slightly more simple explanation as follows:
  33. :   (The Design and Evolution of C++, chapter 10.5 page 216)
  34. :    ....
  35. :     Had a virtual destructor not been used, the cleanup specified in
  36. : Y's (in our case class B) destructor would not have been performed.
  37.  
  38. : Here you see you have choice to use virtual destructor or not. 
  39.  
  40. Although the D&E is written by the inventor of C++ it does not define
  41. the standard. The standard is very clear what is about to happen: If an
  42. object of a derived class is deleted through a pointer to the base
  43. class the base class has to have a virtual destructor, otherwise the
  44. behavior is undefined. This means, the code posted has undefined
  45. behavior. I would call code with undefined behavior to be incorrect.
  46.  
  47. Although the code is likely to work with most implementations, the code
  48. could break (even without any modification) if the memory allocation
  49. procedure uses a simple optimization: For objects with size 1 a buffer
  50. is used to avoid multiple calls to the OS's memory handling system. For
  51. larger objects, the normal memory allocation is used. The deallocation
  52. function is then told the size of the deleted object from its static
  53. size (if there is no virtual destructor) or from its dynamic size (if
  54. there is a virtual destructor). Although such an implementation cannot
  55. be built by a library it can be built by the C++ implementation (the
  56. "normal" global 'operator new()' doesn't get the size of the object;
  57. however, the C++ implementation is free on what to pass to the built-in
  58. 'operator new()')!
  59.  
  60. All which remains is to check is that 'sizeof(A) == 1' and 'sizeof(C) ==
  61. 2' for some implementations (e.g. gcc-2.7.2): You would get some weird
  62. error like an segmentation fault when deleting a 'C' through a pointer
  63. to an 'A'.
  64. --
  65. dietmar.kuehl@uni-konstanz.de
  66. http://www.informatik.uni-konstanz.de/~kuehl/
  67. I am a realistic optimist - that's why I appear to be slightly pessimistic
  68.